﻿using System;
using System.Configuration;
using System.Diagnostics.Contracts;

namespace OpenWaves.ActivityFeed.NHibernate
{
    public class NHibernateSessionFactoryConfiguration: INHibernateSessionFactoryConfiguration
    {
        private const string ActivityFeedConnectionStringName = "ActivityFeed";

        private readonly string _connectionStringName;        

        public NHibernateSessionFactoryConfiguration()
        {
            this._connectionStringName = ActivityFeedConnectionStringName;
        }

        public NHibernateSessionFactoryConfiguration(string connectionStringName)
        {
            Contract.Requires<ArgumentNullException>(!String.IsNullOrEmpty(connectionStringName), "ConnectionStringName can not be empty");
            this._connectionStringName = connectionStringName;
        }

        public string ConnectionString
        {
            get
            {
                var connectionStringSettings = ConfigurationManager.ConnectionStrings[_connectionStringName];                
                Contract.Assert(connectionStringSettings != null, ExceptionMessage.Format("'{0}' connection string has to be assigned in .config file", this._connectionStringName));
                return connectionStringSettings.ConnectionString;
            }
        }
    }
}